{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7ebff6a8",
   "metadata": {},
   "source": [
    "<img src = './三层神经网络.png'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "28e3d389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5],\n",
       "       [11]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "#矩阵乘法\n",
    "E = np.array([[1,2],[3,4]])\n",
    "F = np.array([[1],[2]])\n",
    "np.dot(E,F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "96eb6117",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.3 0.7 1.1]\n",
      "[0.57444252 0.66818777 0.75026011]\n"
     ]
    }
   ],
   "source": [
    "#神经网络前向处理\n",
    "# A = XW + B\n",
    "# 两个输入参数|x1 x2|\n",
    "\n",
    "#二元分类问题\n",
    "def sigmoid(x):\n",
    "    return 1 / (1 + np.exp(-x))\n",
    "\n",
    "# 第一层3个神经元|a1 a2 a3|\n",
    "X = np.array([1.0,0.5])\n",
    "W1 = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])\n",
    "B1 = np.array([0.1,0.2,0.3])\n",
    "A1 = np.dot(X,W1) + B1\n",
    "Z1 = sigmoid(A1)\n",
    "print(A1)\n",
    "print(Z1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "220f9fc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二层2个神经元|a1 a2|  M1*3 dot M3*2 = M1*2\n",
    "# 第一层到第二层的信号传递\n",
    "W2 = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])\n",
    "B2 = np.array([0.1,0.2])\n",
    "A2 = np.dot(Z1,W2) + B2\n",
    "Z2 = sigmoid(A2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f0b1cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#第二层到输出层\n",
    "#两个输出 |y1 y2| M1*2 dot M2*2 = M1*2\n",
    "\n",
    "#恒等函数，回归问题；多元分类问题使用softmax函数\n",
    "def identify_function(x):\n",
    "    return x\n",
    "W3 = np.array([[0.1,0.3],[0.2,0.4]])\n",
    "B3 = np.array([0.1,0.2])\n",
    "A3 = np.dot(Z2,W3) + B3\n",
    "Y = identify_function(A3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dec807f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.31682708 0.69627909]\n"
     ]
    }
   ],
   "source": [
    "#总结\n",
    "#惯例，权重记为大写字母，其他使用小写字母\n",
    "def init_network():\n",
    "    #字典\n",
    "    network = {}\n",
    "    network['W1'] = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])\n",
    "    network['W2'] = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])\n",
    "    network['W3'] = np.array([[0.1,0.3],[0.2,0.4]])\n",
    "    network['b1'] = np.array([0.1,0.2,0.3])\n",
    "    network['b2'] = np.array([0.1,0.2])\n",
    "    network['b3'] = np.array([0.1,0.2])\n",
    "    return network\n",
    "\n",
    "def forward(network,x):\n",
    "    W1,W2,W3 = network['W1'],network['W2'],network['W3']\n",
    "    b1,b2,b3 = network['b1'],network['b2'],network['b3']\n",
    "    a1 = np.dot(x,W1) + b1\n",
    "    z1 = sigmoid(a1)\n",
    "    a2 = np.dot(z1,W2) + b2\n",
    "    z2 = sigmoid(a2)\n",
    "    a3 = np.dot(z2,W3) + b3\n",
    "    y = identify_function(a3)\n",
    "    return y\n",
    "network = init_network()\n",
    "x = np.array([1.0,0.5])\n",
    "y = forward(network,x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "968f5f1e",
   "metadata": {},
   "source": [
    "<img src = './softmax函数.png'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "efe6ef79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y =  [0.01821127 0.24519181 0.73659691] ,y sum =  1.0\n"
     ]
    }
   ],
   "source": [
    "# 输出层的设计\n",
    "# 按数学表达式设计的softmax\n",
    "def softmax(x):\n",
    "    c = np.max(x)\n",
    "    exp = np.exp(x - c)  #避免溢出\n",
    "    sum_exp = np.sum(exp)  #指数函数的和\n",
    "    return exp / sum_exp\n",
    "a = np.array([0.3,2.9,4.0])\n",
    "y = softmax(a)\n",
    "print('y = ',y,',y sum = ',np.sum(y))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0cc54366",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60000, 784)\n",
      "(60000,)\n",
      "(10000, 784)\n",
      "(10000,)\n"
     ]
    }
   ],
   "source": [
    "import sys,os\n",
    "sys.path.insert(1,'../dataset/')\n",
    "from dataset.mnist import load_mnist\n",
    "#(训练图像,训练标签),(测试图像,测试标签)\n",
    "(x_train,t_train),(x_test,t_test) = load_mnist(flatten=True,normalize=False)\n",
    "print(x_train.shape)\n",
    "print(t_train.shape)\n",
    "print(x_test.shape)\n",
    "print(t_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cce9ace3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26e50ddb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
